﻿// CWndProcess.cpp: 实现文件
//

#include "pch.h"
#include "GameHacker.h"
#include "CWndProcess.h"
#include "afxdialogex.h"
#include "tlhelp32.h"
#include "CWndINC.h"



// CWndProcess 对话框

IMPLEMENT_DYNAMIC(CWndProcess, CDialogEx)

CWndProcess::CWndProcess(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROCESS_LST, pParent)
{

}

CWndProcess::~CWndProcess()
{
}

void CWndProcess::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_PROCESSES, m_ProcessLst);
	DDX_Control(pDX, IDC_PROCESSES, m_ProcessLst);
}


BEGIN_MESSAGE_MAP(CWndProcess, CDialogEx)
	ON_NOTIFY(NM_DBLCLK, IDC_PROCESSES, &CWndProcess::OnNMDblclkProcesses)
END_MESSAGE_MAP()


// CWndProcess 消息处理程序


BOOL CWndProcess::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//设置listbox的样式
	long lStyle;
	lStyle = GetWindowLongPtr(m_ProcessLst.m_hWnd, GWL_STYLE);
	lStyle |= LVS_REPORT;
	SetWindowLongPtr(m_ProcessLst.m_hWnd, GWL_STYLE, lStyle);
	lStyle = m_ProcessLst.GetExtendedStyle();
	lStyle |= LVS_EX_FULLROWSELECT;//一次选中一整行
	lStyle |= LVS_EX_GRIDLINES;//设置显示格子线
	m_ProcessLst.SetExtendedStyle(lStyle);

	m_ProcessLst.InsertColumn(0, L"游戏名称", 0, 200);
	m_ProcessLst.InsertColumn(1, L"PID", 0, 400);

	RefreshProcess();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CWndProcess::RefreshProcess()
{
	m_ProcessLst.DeleteAllItems();
	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnap == INVALID_HANDLE_VALUE) {
		AfxMessageBox(L"获取进程列表失败，请检测是否具有管理员权限！");
	}

	PROCESSENTRY32W pe{ sizeof(pe) };
	BOOL bRet = Process32First(hSnap, &pe);
	while (bRet) {
		CString pid, exe;
		pid.Format(L"%d", pe.th32ProcessID);
		exe.Format(L"%s", pe.szExeFile);
		m_ProcessLst.InsertItem(0, pid);
		m_ProcessLst.SetItemText(0, 1, exe);
		bRet = Process32Next(hSnap, &pe);
	}
}



void CWndProcess::OnNMDblclkProcesses(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	NM_LISTVIEW* View = (NM_LISTVIEW*)pNMHDR;
	if (View->iItem != -1) {
		CString pid = m_ProcessLst.GetItemText(View->iItem, 0);
		m_Exe = m_ProcessLst.GetItemText(View->iItem, 1);
		m_dwPid = wcstoul(pid, 0, 10);
		OnOK();
	}
	*pResult = 0;
}
